package com.example.demo.Gmail.controller;


import com.example.demo.Gmail.GmailServiceResponse;
import com.example.demo.Gmail.Util.OSSUtils;
import com.example.demo.Gmail.model.AttchmentModel;
import com.example.demo.Gmail.model.ContextImgModel;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;
@Api(description ="上传文件接口")
@Controller
@RequestMapping("gmail")
public class FileUploadController {
    private final String URL="http://download.files.cnexps.com/";
    /**
     * 正文图片上传到服务器
     * 1.返回图片URL地址
     * 2.对应图片的cid
     * 3.原文件名
     */
        @PostMapping("uploadContextImg")
        @ResponseBody
        @ApiOperation(value = "上传图片" ,  notes="上传图片")
        @ApiResponse(code = 200,message = "上传文件成功",response = ContextImgModel.class)
        public GmailServiceResponse<ContextImgModel> contextImg(@RequestBody MultipartFile[]files) throws IOException {
            GmailServiceResponse response=new GmailServiceResponse();
            if (files==null||files.length==0){
                response.setData(null);
                response.setMsg("目标文件为空，请至少添加一张图片~");
                response.setSuccess(false);
                return response;
            }else {
                for (MultipartFile file : files) {
                    if (!file.getContentType().equals("image/jpeg")) {
                        response.setMsg("正文只支持插入图片文件，请检查");
                        response.setData(null);
                        response.setSuccess(false);
                        return response;
                    }
                }
                List listfile = Lists.newArrayList();
                for (MultipartFile file : files) {
                    Map map= Maps.newHashMap();
                    String originalfileName = file.getOriginalFilename();
                    byte[] fileByteArray = file.getBytes();
                    HttpHeaders headers = new HttpHeaders();
                    headers.setContentType(MediaType.parseMediaType(file.getContentType() + ";charset=UTF-8"));
                    String fileName = UUID.randomUUID().toString()+ originalfileName.substring(originalfileName.lastIndexOf("."), originalfileName.length());
                    String cid = fileName.substring(6, 16);
                    String path = OSSUtils.getDefaultOSS().upLoadFileToOss(fileByteArray, fileName);
                    String imgUrl = URL + path;
                    map.put("imgUrl", imgUrl);
                    map.put("fileName", originalfileName);
                    map.put("cid", cid);
                    listfile.add(map);
                    response.setSuccess(true);
                    response.setMsg("正文添加图片成功");
                    response.setData(listfile);
                }
                return response;
            }
        }
      /**
        * 附件上传到阿里云服务器，返回附件的url地址
     */
    @PostMapping(value = "uploadAttachment")
    @ResponseBody
    @ApiOperation(value = "上传附件",notes = "上传附件")
    public GmailServiceResponse<AttchmentModel> uploadAttachment(@RequestBody MultipartFile[] files) throws IOException {
        GmailServiceResponse response=new GmailServiceResponse();
        if (files==null||files.length==0){
            response.setData(null);
            response.setMsg("目标文件为空，请至少添加一个附件~");
            response.setSuccess(false);
            return response;
        }else {
            List listfile = Lists.newArrayList();
               for (MultipartFile file:files){
                   Map map= Maps.newHashMap();
                   String originalfileName = file.getOriginalFilename();
                   byte [] fileByteArray= file.getBytes();
                   HttpHeaders headers = new HttpHeaders();
                   headers.setContentType(MediaType.parseMediaType(file.getContentType()+";charset=UTF-8"));
                   String fileName = UUID.randomUUID().toString()+"/"+originalfileName;
                   String path= OSSUtils.getDefaultOSS().upLoadFileToOss(fileByteArray,fileName);
                   String fileUrl=URL+path;
                   map.put("fileUrl",fileUrl);
                   map.put("fileName",originalfileName);
                   response.setSuccess(true);
                   response.setMsg("添加附件成功");
                   listfile.add(map);
                   response.setData(listfile);
               }
                return response;
            }
     }


}